package com.jy.admin.rest;

import com.fqgj.common.api.Response;
import com.fqgj.exception.common.ApplicationException;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import com.jy.admin.auth.vo.RequestLocalInfo;
import com.jy.admin.db.dao.OpdUploadRecordDao;
import com.jy.admin.db.entity.OpdUploadRecordEntity;
import com.jy.admin.rest.request.OpdUpdateRequest;
import com.jy.admin.rest.request.OpenDataSaveRequest;
import com.jy.admin.rest.request.ProductUpdateRequest;
import com.jy.admin.rest.response.OpdListResponse;
import com.jy.admin.rest.response.ProductListResponse;
import com.jy.admin.rest.response.ProductSearchRequest;
import com.jy.bo.common.conf.api.ApiResponse;
import com.qianli.cashmama.product.facade.ProductFacade;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import com.robert.vesta.util.IdUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/11/10
 * Time: 11:14 上午
 */
@RestController
@RequestMapping("/open/data")
public class OpenDataController {

    @Autowired
    private OpdUploadRecordDao opdUploadRecordDao;

    @RequestMapping(value = "/save")
    public ApiResponse<Boolean> save(@RequestBody OpenDataSaveRequest request) {
        OpdUploadRecordEntity opdUploadRecordEntity = new OpdUploadRecordEntity()
                .setMerchantCode(RequestLocalInfo.getCurrentUser().getMerchantCode())
                .setNote(request.getNote())
                .setOssUrl(request.getFileUrl())
                .setType(request.getType())
                .setDataNo(IdUtils.genId() + "");
        opdUploadRecordDao.insert(opdUploadRecordEntity);
        return new ApiResponse<Boolean>(true);
    }

    @RequestMapping(value = "/update")
    public ApiResponse<Boolean> update(@RequestBody OpdUpdateRequest request) {
        OpdUploadRecordEntity byDataNo = opdUploadRecordDao.getByDataNo(request.getDataNo());
        if (byDataNo == null) {
            throw new ApplicationException("data not exist");
        }

        if (StringUtils.isNotEmpty(request.getNote())) {
            byDataNo.setNote(request.getNote());
        }

        if (request.getType() != null) {
            byDataNo.setType(request.getType());
        }

        opdUploadRecordDao.updateByPrimaryKey(byDataNo);
        return new ApiResponse<Boolean>(true);
    }

    @RequestMapping(value = "/list")
    public ApiResponse<OpdListResponse> list() {
        String merchantCode = RequestLocalInfo.getCurrentUser().getMerchantCode();
        List<OpdUploadRecordEntity> listByMerchantCode = opdUploadRecordDao.getListByMerchantCode(merchantCode);
        return new ApiResponse<OpdListResponse>(new OpdListResponse(listByMerchantCode));
    }


}
